package com.jstyle.jclifexd.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.model.UserInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/11.
 */

public class BodyFatAdapter extends RecyclerViewBaseAdapter {


    int selectedPosition = -1;

    public BodyFatAdapter(List mDatas) {
        super(mDatas);
        userInfo = NetWorkUtil.getUser();
    }

    UserInfo userInfo;

    @Override
    protected void bindData(BaseViewHolder holder, final int position) {
        BodyFatViewHolder bodyFatViewHolder = (BodyFatViewHolder) holder;
        bodyFatViewHolder.tvBodyFatName.setText(String.valueOf(mDataList.get(position)));
        setLeftDrawable(bodyFatViewHolder.tvBodyFatName, position);

        if (listContent != null) {
            setRightDrawable(bodyFatViewHolder.tvBodyfatValue, position);
            bodyFatViewHolder.tvBodyfatValue.setText(listContent.get(position));
        }
    }

    private void setLeftDrawable(TextView textView, int position) {
        int id = 0;


        switch (position) {
            case 0:
                id = R.drawable.body_water;
                break;
            case 1:
                id = R.drawable.body_slm;
                break;
            case 2:
                id = R.drawable.body_bmc;
                break;
            case 3:
                id = R.drawable.body_pp;
                break;
            case 4:
                id = R.drawable.body_bmr;
                break;
            case 5:
                id = R.drawable.body_vfr;
                break;
            case 6:
                id = R.drawable.body_mc;
                break;
            case 7:
                id = R.drawable.body_fat_control;
                break;
        }
        Drawable drawable = textView.getContext().getResources().getDrawable(id);
        Drawable drawableRight=textView.getContext().getResources().getDrawable(R.drawable.health_tips);
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            drawableRight.setBounds(0, 0, drawableRight.getMinimumWidth(), drawableRight.getMinimumHeight());
            textView.setCompoundDrawables(drawable, null, drawableRight, null);
        }
    }

    private void setRightDrawable(TextView textView, int position) {
        int id = 0;
        if (listContent == null) return;
        String value = listContent.get(position);
        if ("--".equals(value)) {
            textView.setCompoundDrawables(null, null, null, null);
            return;
        }
        switch (position) {
            case 0:


                    float bwValue = Float.valueOf(value.split(" ")[0]);
                    if (userInfo.getGender() == 0) {
                        if (bwValue < 55) {
                            id = R.drawable.body_fat_arrow_down;
                        } else if (bwValue > 65) {
                            id = R.drawable.body_fat_arrow_up;
                        }
                    } else {
                        if (bwValue < 45) {
                            id = R.drawable.body_fat_arrow_down;
                        } else if (bwValue > 60) {
                            id = R.drawable.body_fat_arrow_up;
                        }
                    }

                break;
            case 1:
                float slmValue = Float.valueOf(value.split(textView.getContext().getString(R.string.kg))[0]);
                id = getSlmId(slmValue);
                break;
            case 2:

                break;
            case 3:
                float ppValue = Float.valueOf(value.split(" ")[0]);
                if (ppValue < 16) {
                    id = R.drawable.body_fat_arrow_down;
                } else if (ppValue > 20) {
                    id = R.drawable.body_fat_arrow_up;
                }
                break;
            case 4:

                break;
            case 5:
                float vfrValue = Float.valueOf(value);
                if (vfrValue > 10) {
                    id = R.drawable.body_fat_arrow_up;
                }
                break;
            case 6:

                break;
            case 7:

                break;
        }
        if (id == 0) {
            textView.setCompoundDrawables(null, null, null, null);
            return;
        }
        Drawable drawable = textView.getContext().getResources().getDrawable(id);
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            textView.setCompoundDrawables(null, null, drawable, null);
        }
    }

    private int getSlmId(float slmValue) {
        int id = 0;
        int height = userInfo.getHeight();
        if (userInfo.getGender() == 0) {
            if (height < 160) {
                if (slmValue < 38.5) {
                    id = R.drawable.body_fat_arrow_down;
                } else if (slmValue > 46.5) {
                    id = R.drawable.body_fat_arrow_up;
                }
            } else if (height <= 170) {
                if (slmValue < 44) {
                    id = R.drawable.body_fat_arrow_down;
                } else if (slmValue > 52.4) {
                    id = R.drawable.body_fat_arrow_up;
                }
            } else if (height > 170) {
                if (slmValue < 49.4) {
                    id = R.drawable.body_fat_arrow_down;
                } else if (slmValue > 59.4) {
                    id = R.drawable.body_fat_arrow_up;
                }
            }

        } else {
            if (height < 150) {
                if (slmValue < 29.1) {
                    id = R.drawable.body_fat_arrow_down;
                } else if (slmValue > 34.7) {
                    id = R.drawable.body_fat_arrow_up;
                }
            } else if (height <= 160) {
                if (slmValue < 32.9) {
                    id = R.drawable.body_fat_arrow_down;
                } else if (slmValue > 37.5) {
                    id = R.drawable.body_fat_arrow_up;
                }
            } else if (height > 160) {
                if (slmValue < 36.5) {
                    id = R.drawable.body_fat_arrow_down;
                } else if (slmValue > 43.5) {
                    id = R.drawable.body_fat_arrow_up;
                }
            }
        }
        return id;
    }

    private List<String> listContent;

    public void setContentList(List<String> list) {
        this.listContent = list;
        notifyDataSetChanged();
    }

    @Override
    protected int getLayoutId(int viewType) {

        return R.layout.item_body_fat;
    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder = null;

        View viewOther = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
        baseViewHolder = new BodyFatViewHolder(viewOther);

        return baseViewHolder;
    }

    public class BodyFatViewHolder extends BaseViewHolder {

        @BindView(R.id.tv_bodyFat_name)
        TextView tvBodyFatName;
        @BindView(R.id.tv_bodyfat_value)
        TextView tvBodyfatValue;
        @BindView(R.id.iv_bodyFat_arrow)
        ImageView ivBodyFatArrow;

        public BodyFatViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }



}
